package com.zhiwei.gateway.config;

import com.zhiwei.gateway.route.EurekaRouteLocator;
import com.zhiwei.gateway.route.ZuulSimpleRouteLocator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.netflix.zuul.ZuulProxyAutoConfiguration;
import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.discovery.ServiceRouteMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * function: zuul动态路由
 * author: zhiwei_yang
 * time: 2020/2/10-13:17
 */
@Configuration
@AutoConfigureBefore(ZuulProxyAutoConfiguration.class)
public class ZuulRouteConfig {
    @Autowired
    ZuulProperties zuulProperties;
    @Autowired
    ServerProperties serverProperties;
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired(required = false)
    private Registration registration;
    @Autowired
    private ServiceRouteMapper serviceRouteMapper;

    @Bean
    public SimpleRouteLocator simpleRouteLocator() {
        return new ZuulSimpleRouteLocator(serverProperties.getServlet().getContextPath(), this.zuulProperties);
    }

    @Bean
    public DiscoveryClientRouteLocator discoveryRouteLocator() {
        return new EurekaRouteLocator(this.serverProperties.getServlet().getContextPath(),
                this.discoveryClient, this.zuulProperties, this.serviceRouteMapper,
                this.registration, false);
    }
}
